// pages/teacher/teachersend/teachersend.js
import sendRequest from '../../rpromise/request'
import sendUploadFile from '../../rpromise/uploadfile'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        date: null,
        edate: null,
        imgs: [],
        multiArray: ["校级", "院级"],
        setmeal: ["活动公告", "活动报名"],
        getcount: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "其他"],
        getchecksign:['开启','不开启'],
        checksign:null,
        stype: null,
        selectmeal: null,
        gcount: null,
        gcount2: null,
        resdata: null,
        img: '',
        datalength: 0,
        holdtime: null,
    },

    bindMultiPickerChange: function (e) {
        this.setData({
            stype: this.data.multiArray[e.detail.value]
        })
    },
    bindMealChange: function (e) {
        this.setData({
            selectmeal: this.data.setmeal[e.detail.value]
        })
    },

    bindDateChange: function (e) {
        this.setData({
            date: e.detail.value
        })
    },
    bindEndDateChange(e) {
        this.setData({
            edate: e.detail.value
        })
    },
    bindHoldDateChange(e) {
        this.setData({
            holdtime: e.detail.value
        })
    },
    bindCheckSignChange(e) {
        this.setData({
            checksign: this.data.getchecksign[e.detail.value]
        })
    },
    bindCountChange(e) {
        this.setData({
            gcount2: e.detail.value,
            gcount: e.detail.value + '次'
        })
    },

    //提交
    submit(e) {
        var that = this
        console.log(JSON.stringify(e.detail.value))
        let flag = true
        // console.log()
        if (e.detail.value.atitle.replace(/\s+/g, '') === "") {
            flag = false
        }
        if (e.detail.value.stype.replace(/\s+/g, '') === "") {
            flag = false
        }
        if (e.detail.value.atype.replace(/\s+/g, '') === "") {
            flag = false
        }
        if (e.detail.value.aman.replace(/\s+/g, '') === "") {
            flag = false
        }
        if (e.detail.value.astime.replace(/\s+/g, '') === "") {
            flag = false
        }
        if (e.detail.value.aetime.replace(/\s+/g, '') === "") {
            flag = false
        }
        if (e.detail.value.holdtime.replace(/\s+/g, '') === "") {
            flag = false
        }
        if (e.detail.value.checksign.replace(/\s+/g, '') === "") {
            flag = false
        }
        if (e.detail.value.address.replace(/\s+/g, '') === "") {
            flag = false
        }
        if (e.detail.value.pcount.replace(/\s+/g, '') === "") {
            flag = false
        }
        if (e.detail.value.gcount.replace(/\s+/g, '') === "") {
            flag = false
        }
        if (e.detail.value.tno.replace(/\s+/g, '') === "") {
            flag = false
        }
        if (e.detail.value.acontent.replace(/\s+/g, '') === "") {
            flag = false
        }
        if (flag === false) {
            wx.showToast({
                title: '请将信息填写完整！',
                icon: 'error',
                duration: 2000
            })
            return
        }
        // var ano = Math.floor(Math.random() * 90000000) + 10000000
        let seed = Date.now();
        for (let i = 0; i < 1000000; i++) {
            seed = this.xorshift(seed);
        }
        let randomNum = seed % 100000000;
        if (randomNum.toString().length < 8) {
            randomNum = ('00000000' + randomNum).slice(-8);
        }
        console.log(Math.abs(randomNum))
        e.detail.value.ano = Math.abs(randomNum) + ''
        e.detail.value.gcount = this.data.gcount2
        var data = JSON.stringify(e.detail.value).replace(/"(\w+)"\s*:/g, '$1:').replace(/[{}]/g, '').replace(/[:]/g, '=')
        console.log(data)
        wx.showModal({
            title: '提示',
            content: '确认内容无误吗？',
            complete: (res) => {
                if (res.cancel) {}
                if (res.confirm) {
                    wx.showLoading({
                        title: '提交中...',
                        mask: true,
                    });

                    that.insertData(data)
                }
            }
        })

    },
    async insertData(data) {
        var sql = "INSERT activity SET " + data
        const res1 = await sendRequest({
            method: 'POST',
            url: '/manage',
            data: {
                sql: sql,
            },
        })
        console.log(res1.data.length)
        if (res1.data.length > 0) {
            setTimeout(function () {
                wx.hideLoading()
                wx.showToast({
                    title: '发布成功',
                    icon: 'success',
                })
            }, 2000)

            this.setData({
                date: null,
                edate: null,
                holdtime: ''
            })
        }
    },
    xorshift(seed) {
        let x = seed;
        x ^= x << 13;
        x ^= x >> 17;
        x ^= x << 5;
        return x;
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        wx.setNavigationBarTitle({
            title: '发布活动',
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})